package org.fireflow2.designer.eclipse.figures;

import org.eclipse.draw2d.BorderLayout;
import org.eclipse.draw2d.DelegatingLayout;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.Locator;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.RoundedRectangle;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.ParagraphTextLayout;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.widgets.Display;
import org.fireflow.model.binding.ResourceBinding;
import org.fireflow.pdl.fpdl20.diagram.style.BoundsStyle;
import org.fireflow.pdl.fpdl20.diagram.style.FulfilStyle;
import org.fireflow.pdl.fpdl20.process.Activity;
import org.fireflow2.designer.eclipse.Activator;
import org.fireflow2.designer.eclipse.modelwrapper.basic.ActivityWrapper;
import org.fireflow2.designer.eclipse.util.Util4Eclipse;

public class ActivityFigure extends Figure implements CommonFigureInterface{//extends RoundedRectangle {
	ActivityWrapper activityWrapper = null;

	private MainFigure mainFigure = null;//Activity的可见轮廓
	private HeaderFigure head = new HeaderFigure();
	private TextFlow contentArea = null;//Activity的主标题区域
	private Figure attachedEventPane = null;
	
	public IFigure getAttachedEventPane(){
		return attachedEventPane;
	}
	
	protected boolean useLocalCoordinates() {
		return true;
	}

	public ActivityFigure(ActivityWrapper wrapper) {
		activityWrapper = wrapper;
		this.setLayoutManager(new DelegatingLayout());
		
		//组装activity图形
		mainFigure = new MainFigure();
		Rectangle visibleBounds = wrapper.getBox();
		mainFigure.setPreferredSize(visibleBounds.width,visibleBounds.height);
	    MarginBorder border = new MarginBorder(6,6,6,6);
	    mainFigure.setBorder(border);
	    
	    contentArea  = new TextFlow();
	    contentArea.setLayoutManager(new ParagraphTextLayout(contentArea, ParagraphTextLayout.WORD_WRAP_SOFT));
	    FlowPage flowPage = new FlowPage();
	    flowPage.add(contentArea);
	    flowPage.setHorizontalAligment(PositionConstants.CENTER);
	    

	    BorderLayout layout = new BorderLayout();
	    layout.setVerticalSpacing(5);
	    mainFigure.setLayoutManager(layout);
	    
	    mainFigure.add(head,BorderLayout.TOP);
	    mainFigure.add(flowPage,BorderLayout.CENTER);   
	    MainFigureLocator mainFigureLocator = new MainFigureLocator(this);
	    this.add(mainFigure,mainFigureLocator);
	    
	    //构造边事件区域
	    FlowLayout flowLayout = new FlowLayout(false);
	    flowLayout.setMajorAlignment(FlowLayout.ALIGN_CENTER);
	    flowLayout.setMinorAlignment(FlowLayout.ALIGN_CENTER);
	    attachedEventPane = new Figure();
	    attachedEventPane.setOpaque(false);
	    attachedEventPane.setLayoutManager(flowLayout);
	    this.add(attachedEventPane,new AttachedEventPaneLocator(this));
	}
	public org.eclipse.draw2d.geometry.Rectangle getBox(){
		Rectangle rectangle = activityWrapper.getBox();
		rectangle.x=this.getBounds().x;
		rectangle.y=this.getBounds().y;
		return rectangle;
	}
	
	public Rectangle getSelectionHandleBounds(){		
		Rectangle rectangle = activityWrapper.getBox();
		rectangle.x=0;
		rectangle.y=0;
		
		mainFigure.translateToAbsolute(rectangle);
		rectangle.translate(CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE);
		rectangle.resize(CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE);
		return rectangle;
	}
	
	public void refreshFigure() {		
		Activity theActivity = (Activity)activityWrapper.getWorkflowModelElement();
		org.fireflow.pdl.fpdl20.diagram.basic.Rectangle theRectangle = 
				(org.fireflow.pdl.fpdl20.diagram.basic.Rectangle)activityWrapper.getDiagramElement().getShape();
		
		BoundsStyle boundsStyle = theRectangle.getBounds().getBoundsStyle();
		mainFigure.setLineWidth(boundsStyle.getThick());
		
		int lineStyle = org.eclipse.swt.SWT.LINE_SOLID;
		if (BoundsStyle.LINETYPE_DOTTED.equals(boundsStyle.getLineType())){
			lineStyle = org.eclipse.swt.SWT.LINE_DOT;
		}
		else if (BoundsStyle.LINETYPE_DASHED.equals(boundsStyle.getLineType())){
			lineStyle = org.eclipse.swt.SWT.LINE_DASH;
		}
		else if (BoundsStyle.LINETYPE_DASHDOTTED.equals(boundsStyle.getLineType())){
			lineStyle = org.eclipse.swt.SWT.LINE_DASHDOT;
		}

		mainFigure.setLineStyle(lineStyle);

		mainFigure.setCornerDimensions(new Dimension(boundsStyle.getRadius(),boundsStyle.getRadius()));
	    
	    org.fireflow.pdl.fpdl20.diagram.basic.Label mainLabel = theRectangle.getContentLabel();
	    if (mainLabel==null || mainLabel.getText().equals(org.fireflow.pdl.fpdl20.diagram.basic.Label.CONTENT_FROM_WORKFLOW_ELEMENT)){
		    String displayName = theActivity.getDisplayName();
		    if (displayName==null || displayName.trim().equals("")){
		    	displayName = theActivity.getName();
		    }
		    contentArea.setText(displayName);
	    }else {
	    	contentArea.setText(mainLabel.getText());
	    }
	    if (mainLabel!=null){
		    contentArea.setForegroundColor(Util4Eclipse.colorStr2ColorObj(mainLabel.getFont().getColor()));

		    Font newFont = Util4Eclipse.getFont(mainLabel);
		    
		    contentArea.setFont(newFont);
	    }


	    org.fireflow.pdl.fpdl20.diagram.basic.Label titleLabel = theRectangle.getTitleLabel();
	    if (titleLabel==null || titleLabel.getText().equals(org.fireflow.pdl.fpdl20.diagram.basic.Label.CONTENT_FROM_WORKFLOW_ELEMENT)){
		    ResourceBinding resourceBinding = theActivity.getResourceBinding();
		    if (resourceBinding!=null){
			    String rscBdDisplayname = resourceBinding.getDisplayName();
			    if (rscBdDisplayname!=null && !rscBdDisplayname.trim().equals("")){
			    	head.setText(rscBdDisplayname);
			    }
		    }
	    }else{
	    	head.setText(titleLabel.getText());
	    }

	}
	

	
	class MainFigure extends RoundedRectangle{
		protected void outlineShape(Graphics graphics) {
			Activity theActivity = (Activity)activityWrapper.getWorkflowModelElement();
			org.fireflow.pdl.fpdl20.diagram.basic.Rectangle theRectangle = 
					(org.fireflow.pdl.fpdl20.diagram.basic.Rectangle)activityWrapper.getDiagramElement().getShape();
			
			BoundsStyle boundsStyle = theRectangle.getBounds().getBoundsStyle();
		    Color oldColor = graphics.getForegroundColor();
		    Color newColor = Util4Eclipse.colorStr2ColorObj(boundsStyle.getColor());

		    graphics.setForegroundColor(newColor);
		    super.outlineShape(graphics);
		    graphics.setForegroundColor(oldColor);
		}
		
		protected void fillShape(Graphics graphics) {
			Activity theActivity = (Activity)activityWrapper.getWorkflowModelElement();
			org.fireflow.pdl.fpdl20.diagram.basic.Rectangle theRectangle = 
					(org.fireflow.pdl.fpdl20.diagram.basic.Rectangle)activityWrapper.getDiagramElement().getShape();
			
			int theLineWidth = this.getLineWidth();

			Rectangle r = Rectangle.SINGLETON.setBounds(getBounds());
			r.x += theLineWidth;
			r.y += theLineWidth;
			r.width -= (theLineWidth*2);
			r.height -= (theLineWidth*2);

			Rectangle rect = getBounds();
			Color oldColor = graphics.getBackgroundColor();
			
			FulfilStyle fulfilStyle = theRectangle.getFulfilStyle();
			Color newColor = Util4Eclipse.colorStr2ColorObj(fulfilStyle.getColor());

			String gradientStyle = fulfilStyle.getGradientStyle();
			if (gradientStyle==null || gradientStyle.equals(FulfilStyle.GRADIENT_STYLE_NONE)){
				graphics.setBackgroundColor(newColor);
				
				graphics.fillRoundRectangle(r,
						Math.max(0, corner.width - (int) theLineWidth),
						Math.max(0, corner.height - (int) theLineWidth));
				
				graphics.setBackgroundColor(oldColor);
			}
			else{
				Display disp = Display.getCurrent();
				Color color0 = disp.getSystemColor(SWT.COLOR_WHITE);
				Pattern p = new Pattern(Display.getCurrent(),rect.x,rect.y,rect.x+rect.width,rect.y+rect.height,color0,newColor);
				graphics.setBackgroundPattern(p);
				graphics.fillRoundRectangle(r,
						Math.max(0, corner.width - (int) theLineWidth),
						Math.max(0, corner.height - (int) theLineWidth));
				graphics.setBackgroundPattern(null);
			}

		}
	}
	

	class HeaderFigure extends Figure {
	    private String imgFileName = "resources/obj16/userconfig.png";
	    private ImageFigure imgFigure = null;
	    private Label label;
	    public HeaderFigure() {
		    BorderLayout borderLayout = new BorderLayout();
		    setLayoutManager(borderLayout);
		    
	        label = new Label();
	        
	        this.imgFigure = new ImageFigure();
	        ImageDescriptor imgDesc = ImageDescriptor.createFromFile(Activator.class, imgFileName);
	        imgFigure.setImage(imgDesc.createImage());
	        
	        this.add(imgFigure,BorderLayout.LEFT);
	        add(label,BorderLayout.CENTER);
//		    setBorder(new UnderlineBorder());

	    }
	    public String getText() {
	        return this.label.getText();
	    }
	    public Rectangle getTextBounds() {
	        return this.label.getTextBounds();
	    }
	    public void setText(String text) {
	        this.label.setText(text);
	    }

	}
	
	class UnderlineBorder extends LineBorder{
		public void paint(IFigure figure, Graphics graphics, Insets insets) {
			tempRect.setBounds(getPaintRectangle(figure, insets));
			if (getWidth() % 2 == 1) {
				tempRect.width--;
				tempRect.height--;
			}
			tempRect.shrink(getWidth() / 2, getWidth() / 2);
			graphics.setLineWidth(getWidth());
			if (getColor() != null)
				graphics.setForegroundColor(getColor());
			Point p1 = new Point(tempRect.x,tempRect.y+tempRect.height);
			Point p2 = new Point(tempRect.x+tempRect.width,tempRect.y+tempRect.height);
			graphics.drawLine(p1, p2);
		}
		
	}
	
	class AttachedEventPaneLocator implements Locator{
		ActivityFigure ownerFigure = null;
		public AttachedEventPaneLocator(ActivityFigure ownerFg){
			ownerFigure = ownerFg;
		}
		/* (non-Javadoc)
		 * @see org.eclipse.draw2d.Locator#relocate(org.eclipse.draw2d.IFigure)
		 */
		public void relocate(IFigure target) {
			Rectangle bounds  = ownerFigure.getBounds();
			Rectangle mainFigureBounds = activityWrapper.getBox();
			
			Rectangle tempRect = new Rectangle();
			tempRect.x = 0;
			tempRect.y = mainFigureBounds.height-16;
			tempRect.width = bounds.width;
			tempRect.height = bounds.height-mainFigureBounds.height+16;
			
			target.setBounds(tempRect);
			
		}
		
	}
	
	class MainFigureLocator implements Locator{
		ActivityFigure ownerFigure = null;		
		
		public MainFigureLocator(ActivityFigure ownerFg){
			ownerFigure = ownerFg;

		}
		/* (non-Javadoc)
		 * @see org.eclipse.draw2d.Locator#relocate(org.eclipse.draw2d.IFigure)
		 */
		public void relocate(IFigure target) {			
			Rectangle mainFigureBounds = activityWrapper.getBox();
			mainFigureBounds.x=0;
			mainFigureBounds.y=0;
			target.setBounds(mainFigureBounds);
			
		}
		
	}
}
